package com.yunkaweilai.android.activity.member;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.ScrollView;
import android.widget.Switch;
import android.widget.TextView;

import com.classic.common.MultipleStatusView;
import com.google.gson.Gson;
import com.yunkaweilai.android.R;
import com.yunkaweilai.android.base.BaseActivity;
import com.yunkaweilai.android.constants.Api;
import com.yunkaweilai.android.model.MemberLevelInfo;
import com.yunkaweilai.android.net.HttpRequest;
import com.yunkaweilai.android.net.PostProcess;
import com.yunkaweilai.android.utils.TitleBuilder;
import com.yunkaweilai.android.utils.Utils;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * 修改会员等级
 * Created by Administrator on 2018/6/27.
 */

public class UpdateMemberLevelActivity extends BaseActivity {

    @BindView(R.id.titlebar_iv_left)
    ImageView titlebarIvLeft;
    @BindView(R.id.titlebar_tv_left)
    TextView titlebarTvLeft;
    @BindView(R.id.titlebar_tv)
    TextView titlebarTv;
    @BindView(R.id.titlebar_iv_right_2)
    ImageView titlebarIvRight2;
    @BindView(R.id.titlebar_iv_right)
    ImageView titlebarIvRight;
    @BindView(R.id.titlebar_tv_right)
    TextView titlebarTvRight;
    @BindView(R.id.id_view_underline)
    View idViewUnderline;
    @BindView(R.id.rl_titlebar)
    RelativeLayout rlTitlebar;
    @BindView(R.id.id_edt_member_name)
    EditText idEdtMemberName;
    @BindView(R.id.id_edt_card_price)
    EditText idEdtCardPrice;
    @BindView(R.id.id_password_switch)
    Switch idPasswordSwitch;
    @BindView(R.id.id_password_switch_jici)
    Switch idPasswordSwitchJici;
    @BindView(R.id.id_switch_points)
    Switch idSwitchPoints;
    @BindView(R.id.id_edt_points)
    EditText idEdtPoints;
    @BindView(R.id.id_edt_points2)
    EditText idEdtPoints2;
    @BindView(R.id.id_swithc1)
    Switch idSwithc1;
    @BindView(R.id.id_edt_1)
    EditText idEdt1;
    @BindView(R.id.id_layout1)
    LinearLayout idLayout1;
    @BindView(R.id.id_view1)
    View idView1;
    @BindView(R.id.id_swithc2)
    Switch idSwithc2;
    @BindView(R.id.id_edt_2)
    EditText idEdt2;
    @BindView(R.id.id_layout2)
    LinearLayout idLayout2;
    @BindView(R.id.id_view2)
    View idView2;
    @BindView(R.id.id_swithc3)
    Switch idSwithc3;
    @BindView(R.id.id_layout31)
    LinearLayout idLayout31;
    @BindView(R.id.id_layout32)
    LinearLayout idLayout32;
    @BindView(R.id.id_layout33)
    LinearLayout idLayout33;
    @BindView(R.id.id_layout_point_all)
    LinearLayout idLayoutPointAll;
    @BindView(R.id.id_switch_zhekou)
    Switch idSwitchZhekou;
    @BindView(R.id.id_edt_nums)
    EditText idEdtNums;
    @BindView(R.id.id_layout_zhekou_all)
    LinearLayout idLayoutZhekouAll;
    @BindView(R.id.id_switch_chuzhi)
    Switch idSwitchChuzhi;
    @BindView(R.id.id_layout_chuzhi1)
    LinearLayout idLayoutChuzhi1;
    @BindView(R.id.id_layout_chuzhi2)
    LinearLayout idLayoutChuzhi2;
    @BindView(R.id.id_layout_chuzhi_all)
    LinearLayout idLayoutChuzhiAll;
    @BindView(R.id.id_layout3)
    LinearLayout idLayout3;
    @BindView(R.id.id_switch_chuzhi1)
    Switch idSwitchChuzhi1;
    @BindView(R.id.id_layout_chuzhi0)
    LinearLayout idLayoutChuzhi0;
    @BindView(R.id.content_view)
    ScrollView contentView;
    @BindView(R.id.id_multipleStatusView)
    MultipleStatusView idMultipleStatusView;
    private LayoutInflater inflater;
    private int is_password = 0;//支付密码
    private int is_password_jici = 0;//扣次密码
    private int is_points = 0;//积分功能
    private int is_arrived_points = 0;//积分抵现
    private int is_birthday_points = 0;//会员生日
    private int is_recharge_points = 0;//充值积分规则
    private int is_discount = 0;//折扣功能
    private int is_stored = 0;//储值功能
    private int is_recharge_limit = 0;//储值赠送规则
    private String level_id = "";//会员等级id
    public static final String MEMBER_LEVEL_ID = "MEMBER_LEVEL_ID";
    private MemberLevelInfo memberLevelInfo;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.update_member_level_layout2);
        ButterKnife.bind(this);
        level_id = getIntent().getStringExtra(MEMBER_LEVEL_ID);
        new TitleBuilder(mAc).setLeftImage(R.mipmap.ic_go_back).setLeftOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        }).setRightText("保存").setRightOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                setRight();
            }
        }).setTitleText("修改会员等级");
        inflater = LayoutInflater.from(mAc);
        idMultipleStatusView.showLoading();
        idMultipleStatusView.setOnRetryClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                idMultipleStatusView.showLoading();
                initHttp();
            }
        });
        switchClick();
        initClick();
        initHttp();
    }

    private void initHttp() {
        HttpRequest.postUrl(Api.MEMBER_LEVEL_INFO)
                .addParams("level_id",level_id)
                .execute(new PostProcess.StringCallBack() {

                    @Override
                    public void onResponse(String json) {
                        boolean checkCode = Utils.checkCode(mAc, json);
                        if (checkCode){
                            Gson gson = new Gson();
                            memberLevelInfo = gson.fromJson(json, MemberLevelInfo.class);
                            initView();
                            if (idMultipleStatusView != null){
                                idMultipleStatusView.showContent();
                            }
                        }else{
                            if (idMultipleStatusView != null){
                                idMultipleStatusView.showError();
                            }
                        }
                    }

                    @Override
                    public void onError(Exception e) {
                        if (idMultipleStatusView != null){
                            idMultipleStatusView.showError();
                        }
                    }
                });
    }

    private void initView() {
        MemberLevelInfo.DataBean.ListBean list = memberLevelInfo.getData().getList();
        idEdtMemberName.setText(list.getLevel_name()+"");
        idEdtCardPrice.setText(list.getActivate_amount() + "");
        is_password = list.getIs_password();
        if (list.getIs_password() == 1){
            idPasswordSwitch.setChecked(true);
        }else{
            idPasswordSwitch.setChecked(false);
        }
        if (list.getIs_jici_password() == 1) {
            idPasswordSwitchJici.setChecked(true);
        } else {
            idPasswordSwitchJici.setChecked(false);
        }
        is_points = list.getIs_points();
        if (list.getIs_points() == 1){
            idSwitchPoints.setChecked(true);
        }else{
            idSwitchPoints.setChecked(false);
        }
        idEdtPoints.setText(list.getOpen_points() + "");
        idEdtPoints2.setText(list.getConsumption_points() + "");
        is_arrived_points = list.getIs_arrived_points();
        if (list.getIs_arrived_points() == 1){
            idSwithc1.setChecked(true);
        }else{
            idSwithc1.setChecked(false);
        }
        idEdt1.setText(list.getArrived_amount() + "");
        is_birthday_points = list.getIs_birthday_points();
        if (list.getIs_birthday_points() == 1){
            idSwithc2.setChecked(true);
        }else{
            idSwithc2.setChecked(false);
        }
        idEdt2.setText(list.getMultiple()+"");
        is_discount = list.getIs_discount();
        if (list.getIs_discount() == 1){
            idSwitchZhekou.setChecked(true);
        }else{
            idSwitchZhekou.setChecked(false);
        }
        idEdtNums.setText(list.getDiscount() + "");
        is_stored = list.getIs_stored();
        if (list.getIs_stored() == 1){
            idSwitchChuzhi.setChecked(true);
        }else{
            idSwitchChuzhi.setChecked(false);
        }




        is_recharge_points = list.getIs_recharge_points();
        if (list.getIs_recharge_points() == 1){
            idSwithc3.setChecked(true);
        }else{
            idSwithc3.setChecked(false);
        }
        MemberLevelInfo.DataBean.RulelistBean rulelist = memberLevelInfo.getData().getRulelist();
        if (rulelist.getPoints() != null){
            for (int i = 0; i < rulelist.getPoints().size(); i++) {
                MemberLevelInfo.DataBean.RulelistBean.PointsBean pointsBean = rulelist.getPoints().get(i);
                addView1(pointsBean.getPrice(),pointsBean.getPoints());
            }
        }

        is_recharge_limit = list.getIs_recharge_limit();
        if (list.getIs_recharge_limit() == 1){
            idSwitchChuzhi1.setChecked(true);
        }else{
            idSwitchChuzhi1.setChecked(false);
        }
        if (rulelist.getLimit() != null){
            for (int i = 0; i < rulelist.getLimit().size(); i++) {
                MemberLevelInfo.DataBean.RulelistBean.LimitBean limitBean = rulelist.getLimit().get(i);
                addView2(limitBean.getPrice(),limitBean.getSending_price());
            }
        }




    }

    private void setRight() {
        if (Utils.isEmpty(idEdtMemberName.getText().toString())) {
            showToast("未输入等级名称");
            return;
        }
        if (is_arrived_points == 1) {
            if (Utils.isEmpty(idEdt1.getText().toString())) {
                showToast("未输入积分抵现");
                return;
            }
        }
        showProgressDialog();
        saveHttp();
    }

    private void saveHttp() {
        PostProcess postProcess = HttpRequest.postUrl(Api.ADD_MEMBER_LEVEL)
                .addParams("level_name", idEdtMemberName.getText().toString())
                .addParams("activate_amount", idEdtCardPrice.getText().toString())
                .addParams("validity_type", "permanent")
                .addParams("is_password", is_password + "")
                .addParams("is_jici_password", is_password_jici + "")
                .addParams("is_points", is_points + "")
                .addParams("open_points", idEdtPoints.getText().toString() + "")
                .addParams("consumption_points", idEdtPoints2.getText().toString() + "")
                .addParams("is_recharge_points", is_recharge_points + "")
                .addParams("is_arrived_points", is_arrived_points + "")
                .addParams("arrived_amount", idEdt1.getText().toString() + "")
                .addParams("is_birthday_points", is_birthday_points + "")
                .addParams("multiple", idEdt2.getText().toString() + "")
                .addParams("is_discount", is_discount + "")
                .addParams("discount", idEdtNums.getText().toString() + "")
                .addParams("is_stored", is_stored + "")
                .addParams("id",level_id)
                .addParams("recharge_rule_type", "limit")
                .addParams("is_recharge_limit", is_recharge_limit + "");
        for (int i = 0; i < idLayout32.getChildCount(); i++) {
            View views1 = idLayout32.getChildAt(i);
            EditText edt1 = (EditText) views1.findViewById(R.id.id_edt_1);
            EditText edt2 = (EditText) views1.findViewById(R.id.id_edt_2);
            if (Utils.isEmpty(edt1.getText().toString()) || Utils.isEmpty(edt2.getText().toString())) {

            } else {
                postProcess.addParams("rechargerule[price][" + i + "]", edt1.getText().toString());
                postProcess.addParams("rechargerule[points][" + i + "]", edt2.getText().toString());
            }
        }
        for (int i = 0; i < idLayoutChuzhi1.getChildCount(); i++) {
            View views1 = idLayoutChuzhi1.getChildAt(i);
            EditText edt1 = (EditText) views1.findViewById(R.id.id_edt_1);
            EditText edt2 = (EditText) views1.findViewById(R.id.id_edt_2);
            if (Utils.isEmpty(edt1.getText().toString()) || Utils.isEmpty(edt2.getText().toString())) {

            } else {
                postProcess.addParams("rechargelimit[price][" + i + "]", edt1.getText().toString());
                postProcess.addParams("rechargelimit[sending_price][" + i + "]", edt2.getText().toString());
            }
        }
        postProcess.execute(new PostProcess.StringCallBack() {
            @Override
            public void onResponse(String json) {
                dismissProgressDialog();
                boolean checkCode = Utils.checkCode(mAc, json);
                if (checkCode) {
                    setResult(RESULT_OK);
                    finish();
                }
            }

            @Override
            public void onError(Exception e) {
                dismissProgressDialog();

            }
        });


    }

    //开关状态
    private void switchClick() {
        //是否开启密码
        idPasswordSwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                is_password = isChecked ? 1 : 0;
            }
        });
        //是否开启扣次密码
        idPasswordSwitchJici.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                is_password_jici = isChecked ? 1 : 0;
            }
        });
        //积分功能
        idSwitchPoints.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                is_points = isChecked ? 1 : 0;
                if (isChecked) {
                    idLayoutPointAll.setVisibility(View.VISIBLE);
                } else {
                    idLayoutPointAll.setVisibility(View.GONE);
                }
            }
        });
        //积分抵现
        idSwithc1.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                is_arrived_points = isChecked ? 1 : 0;
                if (isChecked) {
                    idLayout1.setVisibility(View.VISIBLE);
                } else {
                    idLayout1.setVisibility(View.GONE);
                }
            }
        });
        //会员生日
        idSwithc2.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                is_birthday_points = isChecked ? 1 : 0;
                if (isChecked) {
                    idLayout2.setVisibility(View.VISIBLE);
                } else {
                    idLayout2.setVisibility(View.GONE);
                }
            }
        });
        //充值积分规则
        idSwithc3.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                is_recharge_points = isChecked ? 1 : 0;
                if (isChecked) {
                    idLayout3.setVisibility(View.VISIBLE);
                } else {
                    idLayout3.setVisibility(View.GONE);
                }
            }
        });
        //折扣功能
        idSwitchZhekou.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                is_discount = isChecked ? 1 : 0;
                if (isChecked) {
                    idLayoutZhekouAll.setVisibility(View.VISIBLE);
                } else {
                    idLayoutZhekouAll.setVisibility(View.GONE);
                }
            }
        });
        //储值功能
        idSwitchChuzhi.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                is_stored = isChecked ? 1 : 0;
                if (isChecked) {
                    idLayoutChuzhiAll.setVisibility(View.VISIBLE);
                } else {
                    idLayoutChuzhiAll.setVisibility(View.GONE);
                }
            }
        });
        //储值赠送规则
        idSwitchChuzhi1.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                is_recharge_limit = isChecked ? 1 : 0;
                if (isChecked) {
                    idLayoutChuzhi0.setVisibility(View.VISIBLE);
                } else {
                    idLayoutChuzhi0.setVisibility(View.GONE);
                }
            }
        });
    }

    private void initClick() {
        idLayout33.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                addView1("","");
            }
        });

        idLayoutChuzhi2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                addView2("","");
            }
        });
    }

    private void addView1(String a1,String a2) {
        View views = inflater.inflate(R.layout.add_view_guize1, null);
        idLayout32.addView(views);
        ImageView idImgClose = (ImageView) views.findViewById(R.id.id_img_close);
        EditText edt1 = (EditText) views.findViewById(R.id.id_edt_1);
        EditText edt2 = (EditText) views.findViewById(R.id.id_edt_2);
        edt1.setText(a1);
        edt2.setText(a2);
        idImgClose.setTag(views);
        idImgClose.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                View tagView = (View) v.getTag();
                removeViews1(tagView);
            }
        });
    }

    private void addView2(String a1,String a2) {
        View views = inflater.inflate(R.layout.add_view_guize1, null);
        idLayoutChuzhi1.addView(views);
        EditText edt1 = (EditText) views.findViewById(R.id.id_edt_1);
        EditText edt2 = (EditText) views.findViewById(R.id.id_edt_2);
        edt1.setText(a1);
        edt2.setText(a2);
        ImageView idImgClose = (ImageView) views.findViewById(R.id.id_img_close);
        idImgClose.setTag(views);
        idImgClose.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                View tagView = (View) v.getTag();
                removeViews2(tagView);
            }
        });
    }

    private void removeViews1(View views) {
        idLayout32.removeView(views);
    }

    private void removeViews2(View views) {
        idLayoutChuzhi1.removeView(views);
    }
}
